﻿/**
 * 籌碼類
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-4-1 22:57
 */
package com.addcn.texas.utils
{	
	public class  NumberFormat 
	{
		private var _group : String;
		
		function NumberFormat(r_group : String = ",") : void
		{
			this._group = r_group;
		}
		
		/**
		 * 
		 * @param	r_num
		 * @param	r_mill  Boolean 是否将超过亿的转换为 xx.xxE格式
		 * @return
		 */
		public function format(r_num : *, r_mill : Boolean = false) : String
		{
			r_num = String(r_num);
			var _parts:Array = r_num.split(".");  //将整数和小数分割，存入数组
			var _part_0:Array = _parts[0].split(""); //将整数每个字分割存入数组
			var _part_1:Array = (_parts.length > 1) ? _parts[1].split("") : new Array();			
			var _counter:int = 1;
			var _million :String;
			if(r_mill){
				if (_part_0.length >= 9 ) {
					var _n : int = _part_0.length -8;
					_million = r_num.substr(0, _n);				
					_million = _million;		
					r_num = r_num.substr( _n, 2);
					r_num = _million +"." + r_num +"E";
					return r_num;
				}
			}			
			_part_0.reverse();  //倒转数组			
			for(var i:int = 0; i < _part_0.length; i++) {
			  if(_counter > 3) {
				_counter = 0;
				_part_0.splice(i, 0, _group);
			  }
			  _counter++;
			}
			_part_0.reverse();0

			r_num = _part_0.join("");
			if(_parts[1] != null) {
			  r_num += "." + _parts[1];
			}			
			return r_num;
		}
	}
	
}